#!/usr/bin/python
# coding=utf8
#
# Copyright (C) 2011 XNData contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


__author__ = "zhangchitc@gmail.com"


import cookielib
import urllib2, urllib
import xndata.client
from xndata.frontend.info import InfoPageFetcher
from xndata.frontend.info import InfoPageParser

class XNInfoClient (xndata.client.XNClient):

    def __init__ (self, xnclient=None, uname=None, upass=None):
        if xnclient is not None:
            # initialize with already-logined xnclient
            self.opener = xnclient.opener
            self.user_id = xnclient.user_id
            self.user_name = xnclient.user_name

        if (uname is not None) and (upass is not None):
            # initialize with username and password
            xndata.client.XNClient.__init__ (self, uname, upass)


    def getTotalFriend (self, user_id):
        html = InfoPageFetcher.fetch_friend_list_page (self.opener, user_id, 0)
        return InfoPageParser.get_total_friend (html)

    def getTotalFriendList (self, user_id):
        return (self.getTotalFriend (user_id) - 1) / 20 + 1

    def getNextFriendList (self, user_id=None, prev_friend_list=None):
        if user_id is not None:
            html = InfoPageFetcher.fetch_friend_list_page (self.opener, user_id, 0)
            next_friend_list = InfoPageParser.get_friend_list (html, user_id, 0)
        else:
            html = InfoPageFetcher.fetch_friend_list_page (self.opener, 
                        prev_friend_list.user_id, prev_friend_list.cur_page + 1)
            next_friend_list = InfoPageParser.get_friend_list (html, 
                        prev_friend_list.user_id, prev_friend_list.cur_page + 1)
        return next_friend_list

    def getAllFriend (self, user_id):
        total_friend_list = self.getTotalFriendList (user_id)
        all_friend_list = self.getNextFriendList (user_id=user_id)
        prev_res = all_friend_list
        for i in range (1, total_friend_list):
            tmp_friend_list = self.getNextFriendList (prev_friend_list=prev_res)
            all_friend_list.friend_list.extend (tmp_friend_list.friend_list)
            prev_res = tmp_friend_list

        return all_friend_list

    def getProfile (self, user_id):
        html = InfoPageFetcher.fetch_profile_page (self.opener, user_id) 
        profile = InfoPageParser.get_profile (html, user_id)
        
        html = InfoPageFetcher.fetch_contact_page (self.opener, user_id)
        contact = InfoPageParser.get_contact_info (html)
        profile.set_name (contact["name"])
        profile.set_qq (contact["qq"])
        profile.set_phone (contact["phone"])
        profile.set_email (contact["email"])
        return profile


if __name__ == "__main__":
    client = XNInfoClient (uname="zhangchitc@gmail.com", upass="aaaaaaaa")
    
    #print client.getTotalFriend ("236136023")
    
    # Other's albums
    friend_list = client.getNextFriendList (user_id="247780420")
    friend_list1 = client.getNextFriendList (prev_friend_list=friend_list)
    friend_list2 = client.getNextFriendList (prev_friend_list=friend_list1)
    #print friend_list2


    #friend_list = client.getAllFriend (user_id="242124580")
    #print friend_list

    print client.getProfile ( "247780420")

    # other's album
    #photo_list = client.getNextPhotoList (user_id="247780420", album_id="282652960")
    #print photo_list
    # my album
    #photo_list = client.getNextPhotoList (user_id="242124580", album_id="432244996")
    #print photo_list
    #print client.getAllPhoto ("242124580", "432244996")
    #print client.getAllPhoto ("247780420", "282652960")
    # print client.getAllPhoto ("252475509", "265669703")
    #print client.getAllPhoto ("242124580", "272296924")
